const db = require('../db/index');
//----------------------------------------------------------------
// 获取所有信息
exports.deviceList = (req, res) => {
    var pagenum = req.query.pagenum; //页码值
    var pagesize = req.query.pagesize; //每页显示数据
    var device_name = req.query.device_name;
    var device_serial = req.query.device_serial;
    var applicat_name = req.query.applicat_name;
    // console.log(applicat_name);
    // console.log(device_name);
    // console.log(device_serial);
    const count_value = (pagenum - 1) * pagesize; //查询的起始值
    var sql = null;
    if (device_name) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.applicat from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 0 and lab_device.device_name like "%${device_name}%" `
    }
    if (device_serial) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.applicat from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 0 and lab_device.device_serial like "${device_serial}%" `
    }
    if (applicat_name) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.applicat from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 0 and lab_device.applicat like "%${applicat_name}%" `
    }
    if (device_name && device_serial) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.applicat from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 0 and lab_device.device_name like "%${device_name}%" and lab_device.device_serial like "%${device_serial}%" `
    }
    if (device_name && applicat_name) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.applicat from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 0 and lab_device.device_name like "%${device_name}%" and lab_device.applicat like "%${applicat_name}%" `
    }
    if (device_serial && applicat_name) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.applicat from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 0 and lab_device.device_serial like "${device_serial}%" and lab_device.applicat like "%${applicat_name}%" `
    }
    if (device_name && device_serial && applicat_name) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.applicat from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 0 and lab_device.device_name like "%${device_name}%" and lab_device.device_serial like "%${device_serial}%" and lab_device.applicat like "%${applicat_name}%" `
    }
    if (!device_name && !device_serial && !applicat_name) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.applicat from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 0 `
    }
    const countStr = `select count(*) as total from(${sql}) as labDevice `;
    db.query(countStr, (err, results) => {
        // console.log(results);
        if (err) return res.cc(err);
        const sum = results[0].total;
        // console.log(sum);
        sql += `limit ${count_value}, ${pagesize}`;
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            if (results.length == 0) return res.cc('没有相关信息！')
            // console.log(results);
            res.send({
                status: 0,
                message: '获取信息成功！',
                data: results,
                total: sum,
            })
        })
    })
}
// ----------------------------------------------------------------
// 添加信息
exports.addDevice = (req, res) => {
    // 手动判断是否上传了图片
    if (!req.file || req.file.fieldname !== 'device_picture') return res.cc('图片是必需参数！')
    // 导入处理路径的 path 核心模块
    const path = require('path')

    // console.log(req.body) // 文本类型的数据
    // console.log('--------分割线----------')
    // console.log(req.file) // 文件类型的数据

    const device_serial = req.body.device_serial;
    const device_cate_id = req.body.device_cate_id;
    const device_picture = req.file.filename;
    // console.log(device_picture);
    const data = {
        ...req.body,
        device_time: new Date(),
        device_picture: path.join('/uploads', req.file.filename),
    }
    const sql = `select * from lab_device where device_serial = ? and device_cate_id = ? and is_delete = 0 `
    db.query(sql, [device_serial, device_cate_id], (err, results) => {
        if (err) return res.cc(err)
        if (results.length > 0) return res.cc("该分类下的序列号重复，请检查更换后重试！")
        const sql = `insert into lab_device set ? `
        db.query(sql, data, (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('添加失败！')
            res.cc('添加成功！', 0)
        })
    })
}
// ----------------------------------------------------------------
// 获取分类和信息
exports.getcateAddLab = (req, res) => {
    const cateSql = `select * from lab_device_cate where is_delete = 0`
    db.query(cateSql, (err, result) => {
        if (err) return res.cc(err)
        if (result.length == 0) return res.cc('没有相关信息！')
        // console.log(results);
        const labSql = `select * from lab_class where is_delete = 0 `
        db.query(labSql, (err, results) => {
            if (err) return res.cc(err)
            if (results.length == 0) return res.cc('没有相关信息！')
            res.send({
                status: 0,
                message: '获取和信息成功！',
                dataCate: result,
                dataLab: results,
            })
        })
    })
}
// ----------------------------------------------------------------
// 根据性别获取楼栋
exports.getLou = (req, res) => {
    const sex = req.params.sex;
    // console.log(sex);
    var lou_type = null;
    if (sex === '女生') {
        lou_type = '女生楼'
    }
    if (sex === '男生') {
        lou_type = '男生楼'
    }
    // console.log(lou_type);
    const cateSql = `select * from lab_device_cate where is_delete = 0 and lou_type = ? `
    db.query(cateSql, lou_type, (err, result) => {
        if (err) return res.cc(err)
        const labSql = `select * from lab_class where is_delete = 0 `
        db.query(labSql, (err, results) => {
            if (err) return res.cc(err)
            if (results.length == 0) return res.cc('没有相关信息！')
            res.send({
                status: 0,
                message: '获取和信息成功！',
                dataCate: result,
                dataLab: results,
            })
        })
    })
}
//----------------------------------------------------------------
// 根据id获取信息
exports.getDevice = (req, res) => {
    const id = req.params.id;
    // console.log(id);
    // const sql = `select * from lab_device where id = ? and is_delete = 0 `
    const sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status from lab_device
                 inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
                 inner join lab_class on lab_device.lab_class_id = lab_class.id
                 where lab_device.id = ? and lab_device.is_delete = 0 `
    db.query(sql, id, (err, results) => {
        // console.log(results);
        if (err) return res.cc(err)
        if (results.length === 0) return res.cc('获取本信息失败！')
        res.send({
            status: 0,
            message: '获取信息成功！',
            data: results,
        })
    })
}
//----------------------------------------------------------------
// 根据id修改信息
exports.editDevice = (req, res) => {
    // 手动判断是否上传了图片
    if (!req.file || req.file.fieldname !== 'device_pic') return res.cc('图片是必需参数！')
    // 导入处理路径的 path 核心模块
    const path = require('path')
    // console.log(req.body) // 文本类型的数据
    // console.log('--------分割线----------')
    // console.log(req.file) // 文件类型的数据

    const id = req.body.id;
    const device_serial = req.body.device_serial;
    const device_cate_id = req.body.device_cate_id;
    const device_picture = req.file.filename;
    console.log(device_picture);
    const data = {
        ...req.body,
        device_picture: path.join('/uploads', req.file.filename),
    }
    const sql = `select * from lab_device where id <> ? and device_serial = ? and device_cate_id = ? and is_delete = 0 `
    db.query(sql, [id, device_serial, device_cate_id], (err, results) => {
        if (err) return res.cc(err);
        if (results.length === 1) return res.send({
            status: 1,
            message: '该分类下序列号重复，请更换后重试！'
        })
        if (results.length === 1 && results[0].device_serial === device_serial) return res.cc('该分类下序列号重复，请更换后重试！', 1);
        const sql = `update lab_device set ? where id = ? `
        // console.log(req.body);
        db.query(sql, [data, id], (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('修改信息失败！')
            res.cc('修改信息成功！', 0)
        })
    })
}
// ----------------------------------------------------------------
// 根据id审核
exports.auditDevice = (req, res) => {
    const id = req.params.id;
    // console.log(id);
    const sql = `update lab_device set device_status = 2 where id = ?`
    db.query(sql, id, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('审核失败')
        res.cc('审核成功', 0)
    })
}
//----------------------------------------------------------------
// 根据id维修
exports.repairDevice = (req, res) => {
    const id = req.query.id;
    const repair_reason = req.query.repair_reason;
    const start_repair_time = new Date();
    // console.log(id);
    const sql = `update lab_device set device_status = 3,repair_reason = ?,start_repair_time = ? where id = ?`
    db.query(sql, [repair_reason, start_repair_time, id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('失败')
        res.cc('成功', 0)
        const sql = `insert into repair_log (device_name,device_price,device_maker,device_serial,cate_name,lab_class_name,device_picture,repair_reason,start_repair_time)
                     select lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_picture,lab_device.repair_reason ,lab_device.start_repair_time from lab_device
                     inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
                     inner join lab_class on lab_device.lab_class_id = lab_class.id
                     where lab_device.is_delete = 0 and lab_device.id = ? `
        db.query(sql, id, (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('维修记录插入失败')
        })
    })
}
//----------------------------------------------------------------
// 根据id恢复使用
exports.recoverDevice = (req, res) => {
    const id = req.params.id;
    // console.log(id);
    const sql = `select lab_device.device_name,lab_device.device_serial ,lab_device_cate.cate_name ,lab_device.repair_reason from lab_device
                 inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
                 inner join lab_class on lab_device.lab_class_id = lab_class.id
                 where lab_device.is_delete = 0 and lab_device.id = ? `
    db.query(sql, id, (err, results) => {
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc('没有相关信息！')
        const device_name = results[0].device_name;
        const device_serial = results[0].device_serial;
        const cate_name = results[0].cate_name;
        const repair_reason = results[0].repair_reason;
        // console.log(device_name);
        // console.log(device_serial);
        // console.log(cate_name);
        // console.log(repair_reason);
        const date = new Date();
        const sql = `update repair_log set end_repair_time = ? where device_name = '${device_name}' and device_serial = '${device_serial}' and cate_name = '${cate_name}' and repair_reason = '${repair_reason}' and end_repair_time is null `
        db.query(sql, date, (err, result) => {
            if (err) return res.cc(err)
            if (result.affectedRows !== 1) return res.cc('插入结束维修时间失败')
            // console.log('ok');
            const sql = `update lab_device set device_status = 0 where id = ?`
            db.query(sql, id, (err, results) => {
                if (err) return res.cc(err)
                if (results.affectedRows !== 1) return res.cc('失败')
                res.cc('成功', 0)
            })

        })
    })


}
//----------------------------------------------------------------
// 根据id报废
exports.deleteDevice = (req, res) => {
    const id = req.params.id;
    const delete_time = new Date();
    // console.log(id);
    const sql = `update lab_device set is_delete = 1,delete_time = ? where id = ?`
    db.query(sql, [delete_time, id], (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('失败')
        res.cc('成功', 0)
    })
}
//----------------------------------------------------------------
// 获取所有报废信息
exports.deleteDeviceList = (req, res) => {
    var pagenum = req.query.pagenum; //页码值
    var pagesize = req.query.pagesize; //每页显示数据
    var device_name = req.query.device_name;
    var device_serial = req.query.device_serial;
    // console.log(device_name);
    // console.log(device_serial);
    const count_value = (pagenum - 1) * pagesize; //查询的起始值
    var sql = null;
    if (device_name) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.delete_time from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 1 and lab_device.device_name like "%${device_name}%" `
    }
    if (device_serial) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.delete_time from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 1 and lab_device.device_serial like "${device_serial}%" `
    }
    if (device_name && device_serial) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.delete_time from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 1 and lab_device.device_name like "%${device_name}%" and lab_device.device_serial like "%${device_serial}%" `
    }
    if (!device_name && !device_serial) {
        sql = `select lab_device.id,lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_time ,lab_device.device_picture,lab_device.device_status,lab_device.delete_time from lab_device
               inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
               inner join lab_class on lab_device.lab_class_id = lab_class.id
               where lab_device.is_delete = 1 `
    }
    const countStr = `select count(*) as total from(${sql}) as labDeleteDevice `;
    db.query(countStr, (err, results) => {
        // console.log(results);
        if (err) return res.cc(err);
        const sum = results[0].total;
        // console.log(sum);
        sql += `limit ${count_value}, ${pagesize}`;
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            if (results.length == 0) return res.cc('没有相关信息！', 1)
            res.send({
                status: 0,
                message: '获取报废信息成功！',
                data: results,
                total: sum,
            })
        })
    })
}
//----------------------------------------------------------------
// 获取所有维修日志信息
exports.repairDeviceList = (req, res) => {
    var pagenum = req.query.pagenum; //页码值
    var pagesize = req.query.pagesize; //每页显示数据
    var device_name = req.query.device_name;
    var device_serial = req.query.device_serial;
    // console.log(device_name);
    // console.log(device_serial);
    const count_value = (pagenum - 1) * pagesize; //查询的起始值
    var sql = null;
    if (device_name) {
        sql = `select * from repair_log where device_name like '%${device_name}%' `
    }
    if (device_serial) {
        sql = `select * from repair_log where device_serial like '%${device_serial}%' `
    }
    if (device_name && device_serial) {
        sql = `select * from repair_log where device_name like '%${device_name}%' and device_serial like '%${device_serial}%' `
    }
    if (!device_name && !device_serial) {
        sql = `select * from repair_log order by id desc `
    }
    const countStr = `select count(*) as total from(${sql}) as repairDevice `;
    db.query(countStr, (err, results) => {
        // console.log(results);
        if (err) return res.cc(err);
        const sum = results[0].total;
        // console.log(sum);
        sql += `limit ${count_value}, ${pagesize}`;
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            if (results.length == 0) return res.cc('没有相关信息！', 1)
            res.send({
                status: 0,
                message: '获取维修信息成功！',
                data: results,
                total: sum,
            })
        })
    })
}
//----------------------------------------------------------------
// 根据id借用
exports.applyDevice = (req, res) => {
    const id = req.params.id;
    const username = req.user.username;
    // console.log(id);
    const sql = `update lab_device set device_status = 1,applicat = '${username}' where id = ? `
    db.query(sql, id, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('借用失败')
        res.cc('借用成功', 0)
    })

}
//----------------------------------------------------------------
// 根据id审核借用
exports.auditApplyDevice = (req, res) => {
    const id = req.params.id;
    const sql = `select lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_picture,lab_device.applicat from lab_device
                 inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
                 inner join lab_class on lab_device.lab_class_id = lab_class.id
                 where lab_device.is_delete = 0 and lab_device.id = ? `
    db.query(sql, id, (err, results) => {
        // console.log(results);
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc('没有相关信息！')
        const device_name = results[0].device_name;
        const device_price = results[0].device_price;
        const device_maker = results[0].device_maker;
        const device_serial = results[0].device_serial;
        const cate_name = results[0].cate_name;
        const lab_class_name = results[0].lab_class_name;
        const device_picture = results[0].device_picture;
        // console.log(device_picture);
        const applicat = results[0].applicat;
        const start_time = new Date();
        // console.log(start_time);
        const sql = `insert into borrow_log (device_name,device_price,device_maker,device_serial,cate_name,lab_class_name,device_picture,applicat,start_time) values ('${device_name}','${device_price}','${device_maker}','${device_serial}','${cate_name}','${lab_class_name}',?,'${applicat}',?) `
        db.query(sql, [device_picture, start_time], (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('失败！')
            const sql = `update lab_device set device_status = 2 where id = ? `
            db.query(sql, id, (err, results) => {
                if (err) return res.cc(err)
                if (results.affectedRows !== 1) return res.cc('审核失败')
                res.cc('审核成功', 0)
            })
        })
    })



}
//----------------------------------------------------------------
// 根据id归还借用
exports.returnDevice = (req, res) => {
    const id = req.params.id;
    const username = req.user.username;
    // console.log(username);
    // console.log(id);
    const sql = `select lab_device.device_name ,lab_device.device_price ,lab_device.device_maker ,lab_device.device_serial ,lab_device_cate.cate_name ,lab_class.lab_class_name ,lab_device.device_picture,lab_device.applicat from lab_device
                 inner join lab_device_cate on lab_device.device_cate_id = lab_device_cate.id
                 inner join lab_class on lab_device.lab_class_id = lab_class.id
                 where lab_device.is_delete = 0 and lab_device.id = ? `
    db.query(sql, id, (err, results) => {
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc('没有相关信息！')
        const device_name = results[0].device_name;
        const device_serial = results[0].device_serial;
        const end_time = new Date();
        const sql = `update borrow_log set end_time = ? where device_name = '${device_name}' and device_serial = '${device_serial}' and applicat = '${username}' and end_time is null `
        db.query(sql, end_time, (err, results) => {
            if (err) return res.cc(err)
            if (results.affectedRows !== 1) return res.cc('请借用人归还此')
            // console.log('okk');
            const sql = `update lab_device set device_status = 0,applicat = Null where id = ? `
            db.query(sql, id, (err, results) => {
                if (err) return res.cc(err)
                if (results.affectedRows !== 1) return res.cc('审核失败')
                res.cc('归还成功', 0)
            })

        })
    })
}
//----------------------------------------------------------------
// 获取所有借用日志
exports.borrowDeviceLog = (req, res) => {
    var pagenum = req.query.pagenum; //页码值
    var pagesize = req.query.pagesize; //每页显示数据
    var device_name = req.query.device_name;
    var device_serial = req.query.device_serial;
    var applicat_name = req.query.applicat_name;
    // console.log(applicat_name);
    // console.log(device_name);
    // console.log(device_serial);
    const count_value = (pagenum - 1) * pagesize; //查询的起始值
    var sql = null;
    if (device_name) {
        sql = `select * from borrow_log where device_name like '%${device_name}%' `
    }
    if (device_serial) {
        sql = `select * from borrow_log where device_serial like '%${device_serial}%' `
    }
    if (applicat_name) {
        sql = `select * from borrow_log where applicat like '%${applicat_name}%' order by id desc `
    }
    if (device_name && device_serial) {
        sql = `select * from borrow_log where device_name like '%${device_name}%' and device_serial like '%${device_serial}%' `
    }
    if (device_name && applicat_name) {
        sql = `select * from borrow_log where device_name like '%${device_name}%' and applicat like '%${applicat_name}%' `
    }
    if (device_serial && applicat_name) {
        sql = `select * from borrow_log where device_serial like '%${device_serial}%' and applicat like '%${applicat_name}%' `
    }
    if (device_name && device_serial && applicat_name) {
        sql = `select * from borrow_log where device_name like '%${device_name}%' and device_serial like '%${device_serial}%' and applicat like '%${applicat_name}%' `
    }
    if (!device_name && !device_serial && !applicat_name) {
        sql = `select * from borrow_log order by id desc `
    }
    const countStr = `select count(*) as total from(${sql}) as borrowDeviceLog `;
    db.query(countStr, (err, results) => {
        // console.log(results);
        if (err) return res.cc(err);
        const sum = results[0].total;
        // console.log(sum);
        sql += `limit ${count_value}, ${pagesize}`;
        db.query(sql, (err, results) => {
            if (err) return res.cc(err)
            if (results.length == 0) return res.cc('没有相关信息！', 1)
            res.send({
                status: 0,
                message: '获取维修信息成功！',
                data: results,
                total: sum,
            })
        })
    })
}
//----------------------------------------------------------------
// 根据id查看
exports.viewDevice = (req, res) => {
    const id = req.params.id;
    const sql = `select device_picture from lab_device where id = ? `
    db.query(sql, id, (err, results) => {
        // console.log(results);
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc('没有相关信息！')
        res.send({
            status: 0,
            message: '获取图片信息成功！',
            data: results,
        })
    })
}

//----------------------------------------------------------------
// 根据id查看图片——借用日志表
exports.viewBorrowDeviceLog = (req, res) => {
    const id = req.params.id;
    const sql = `select device_picture from borrow_log where id = ? `
    db.query(sql, id, (err, results) => {
        // console.log(results);
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc('没有相关信息！')
        res.send({
            status: 0,
            message: '获取图片信息成功！',
            data: results,
        })
    })
}
//----------------------------------------------------------------
// 根据id查看图片——报修日志表
exports.viewRepairDeviceLog = (req, res) => {
    const id = req.params.id;
    const sql = `select device_picture from repair_log where id = ? `
    db.query(sql, id, (err, results) => {
        // console.log(results);
        if (err) return res.cc(err)
        if (results.length == 0) return res.cc('没有相关信息！')
        res.send({
            status: 0,
            message: '获取图片信息成功！',
            data: results,
        })
    })
}